clear all 
set more off 
set maxvar 15000 
clear matrix

    
	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if edu_sample==1 & decade<1950
    local weight "wgt_sex_race_edu"
    
* Generate interaction term for decline
    egen time = group(dob)
    replace time = time-1
    
    gen dad_inc=.
    gen interaction=.

*-----------------------*
* PANEL A (LOGS)
*-----------------------*

    estimates clear 
    
    foreach var of varlist log_father_byocc_interp_CWfix log_father_byrace_interp_CWfix log_father_interpolated_CWfix log_father_byors_edu_interp {
        replace dad_inc = `var'
        replace interaction = `var'*time
        
        eststo: reghdfe log_son_baseline dad_inc interaction [pw=`weight'], vce(robust) abs(time)
    }

    label var interaction "Inc. x (Year-1910)"
    label var dad_inc "Parental income"

    #delimit ;
    esttab using "$Mydirectory2/appendix_b/Table_log_decline_measurementerror.tex", replace booktabs star(* 0.10 ** 0.05 *** 0.01) label  
    keep(dad_inc interaction) nobase noomit nonote alignment(c c) se(4) b(4) brackets   
    mtitle("Occupation" "Occ. x race" "\shortstack{Occ. x race \\ x South}" "\shortstack{Occ. x race \\ x South x edu}")
    stats(N, fmt(%9.0gc) label("Observations")) ;
    #delimit cr
    
*-----------------------*
* PANEL B (RANKS)
*-----------------------*
    estimates clear 
    
    foreach var of varlist rank_father_byocc_edusample rank_father_byrace_edusample rank_father_CW_edusample rank_father_byr_bys_edu {
        replace dad_inc = `var'
        replace interaction = `var'*time
        
        eststo: reghdfe rank_son_baseline dad_inc interaction [pw=`weight'], vce(robust) abs(time)
    }

    label var interaction "Rank x (Year-1910)"
    label var dad_inc "Parental rank"

    #delimit ;
    esttab using "$Mydirectory2/appendix_b/Table_rank_decline_measurementerror.tex", replace booktabs star(* 0.10 ** 0.05 *** 0.01) label  
    keep(dad_inc interaction) nobase noomit nonote alignment(c c) se(4) b(4) brackets   
    mtitle("Occupation" "Occ. x race" "\shortstack{Occ. x race \\ x South}" "\shortstack{Occ. x race \\ x South x edu}")
    stats(N, fmt(%9.0gc) label("Observations")) ;
    #delimit cr
